<template>
  <div>
    <div class="kylin-form configure-confirm-table">
      <ky-table
        :search_data="search_data"
        :getData="getNoExistCve"
        :search_criteria="search_criteria"
        :showSelect="true"
        :modalAppendToBody="false"
        :appendToBody="true"
        :modalTable="true"
        ref="table"
      >
        <template v-slot:table-action>
          <el-button
            class="kylin-item-button"
            @click="handleAdd"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
            >{{ $t("configure.add") }}
          </el-button>
        </template>
        <template v-slot:table>
          <el-table-column prop="cveId" :label="$t('patch.CVE_number')">
          </el-table-column>
          <el-table-column prop="sysNum" :label="$t('patch.influence_system')">
          </el-table-column>
          <el-table-column prop="severity" :label="$t('patch.risk_level')">
          </el-table-column>
          <el-table-column prop="status" :label="$t('system.status')">
          </el-table-column>
          <el-table-column prop="issued_date" :label="$t('patch.publish_time')">
          </el-table-column>
        </template>
      </ky-table>
    </div>
  </div>
</template>

<script>
import kyTable from "@/components/KyTable";
import { createGroupCve, getNoExistCve } from "@/api/cve_group";
export default {
  props: {
    id: {
      type: Number | String,
    },
    form: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  components: {
    kyTable,
  },
  data() {
    return {
      loading: false,
      search_criteria: {
        sortBy: "id",
        sortOrder: "desc",
        id: this.$route.params.id,
      },
      search_data: {
        cveId: {
          type: "text",
          name: "patch.CVE_number",
        },
        severity: {
          type: "select",
          name: "patch.risk_level",
          items: [
            { key: "Low", name: "低风险" },
            { key: "Moderate", name: "中风险" },
            { key: "Important", name: "高风险" },
            { key: "Critical", name: "严重风险" },
          ],
        },
        issued_date: {
          type: "date",
          name: "patch.publish_time",
        },
      },
    };
  },
  methods: {
    getNoExistCve,
    async getSelectAllData() {
      let data = [];
      const res = await this.getCveList({
        ...this.$route.query,
        ...this.search_criteria,
        paged: false,
      });
      res.data.data.results.map((item) => {
        data.push({
          id: item.id,
          cveId: item.cveId,
        });
      });
      return data;
    },
    async handleAdd() {
      let data = [];
      if (this.$refs.table.select_id == "2") {
        data = await this.getSelectAllData();
      } else {
        this.$refs.table.selectRow.rows.forEach((item) => {
          data.push({
            id: item.id,
            cveId: item.cveId,
          });
        });
      }
      createGroupCve({ id: this.id, data }).then((res) => {
        if (res.data.code === "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
          this.$emit("click", "success");
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      });
    },
    handleCancel() {
      this.$emit("click");
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.configure-confirm-table {
  max-height: 600px;
  overflow: auto;
}
</style>